package com.siwei.mes.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.jiguang.common.utils.StringUtils;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.CheckQuickConfig;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.system.SupplierCompanyMaterials;
import com.siwei.mes.enums.CheckEnum;
import com.siwei.mes.enums.CheckTypeEnum;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.experiment.CheckBatchConfigMapper;
import com.siwei.mes.mapper.experiment.CheckQuickConfigMapper;
import com.siwei.mes.mapper.materials.MaterialsMapper;
import com.siwei.mes.mapper.materials.SupplierCompanyMaterialsMapper;
import com.siwei.mes.service.experiment.ExperimentCheckTypeRecordService;
import com.siwei.mes.service.experiment.ExperimentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 根据检测类型生成原材料实验台账 月检、季检、半年检、年检
 *
 * @program: mes-platform
 * @description:
 * @author: yangke
 * @create: 2024-07-28 11:45
 **/
@Component
public class RawMaterialsExperimentNewJob {
    private static final Logger log = LoggerFactory.getLogger("job");
    @Resource
    private ExperimentCheckTypeRecordService experimentCheckTypeRecordService;
    @Resource
    private CheckQuickConfigMapper checkQuickConfigMapper;
    @Resource
    private CheckBatchConfigMapper checkBatchConfigMapper;
    @Resource
    private ExperimentService experimentService;
    @Resource
    private MaterialsMapper materialsMapper;
    @Resource
    private SupplierCompanyMaterialsMapper supplierCompanyMaterialsMapper;

    /**
     * 获取所有快检指标配置
     *
     * @param checkType 检测类型 0-常规 10-月检  20-季检  30-半年检  40-年检
     * @return
     */
    public List<CheckQuickConfig> getQuickConfigList(Integer checkType, Integer projectCategory) {
        CheckQuickConfig quickConfig = new CheckQuickConfig();
        quickConfig.setProjectCategory(projectCategory);
        quickConfig.setCheckType(checkType);
        return checkQuickConfigMapper.getList(quickConfig);
    }

    /**
     * 获取所有批检指标配置  混凝土这里批检只有抗压强度
     *
     * @return
     */
    public List<CheckBatchConfig> getBatchConfig(Integer checkType, Integer projectCategory) {
        CheckBatchConfig batchConfig = new CheckBatchConfig();
        batchConfig.setProjectCategory(projectCategory);
        batchConfig.setCheckType(checkType);
        return checkBatchConfigMapper.getList(batchConfig);
    }

    /**
     * 每个月1号生成月检的
     */
    //@Scheduled(cron = " 0 0 1 1 * *")
    public void genMonth() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.MONTH_CHECK;
        log.info("【原材料】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        for (MaterialsEnum materialsEnum : MaterialsEnum.values()) {
            if (!MaterialsEnum.CONCRETE.getCode().equals(materialsEnum.getCode())) {
                List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (quickConfigList.isEmpty()) {
                    log.info("【原材料快检】【{}】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料快检试验委托*/
                    this.createQuickMaterialsExperiment(checkTypeEnum, quickConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
                /**这里是批检的*/
                List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (batchConfigList.isEmpty()) {
                    log.info("【原材料批检】【{}】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料批检试验委托*/
                    this.createBatchMaterialsExperiment(checkTypeEnum, batchConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
            }
        }
        log.info("【原材料】{}定时任务执行完毕！", checkTypeEnum.getName());
    }

    /**
     * 1,4,7,10 的1号生成季检
     */
    //@Scheduled(cron = " 0 1 1 1 1,4,7,10 *")
    public void genQuarter() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.QUARTER_CHECK;
        log.info("【原材料】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        for (MaterialsEnum materialsEnum : MaterialsEnum.values()) {
            if (!MaterialsEnum.CONCRETE.getCode().equals(materialsEnum.getCode())) {
                List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (quickConfigList.isEmpty()) {
                    log.info("【原材料快检】【{}】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料快检试验委托*/
                    this.createQuickMaterialsExperiment(checkTypeEnum, quickConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
                /**这里是批检的*/
                List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (batchConfigList.isEmpty()) {
                    log.info("【原材料批检】【{}】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料批检试验委托*/
                    this.createBatchMaterialsExperiment(checkTypeEnum, batchConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
            }
        }
        log.info("【原材料】{}定时任务执行完毕！", checkTypeEnum.getName());
    }

    /**
     * 1,7 的1号生成半年检
     */
    //@Scheduled(cron = " 0 0 1 1 1,7 *")
    public void genHalfYear() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.HALF_YEAR_CHECK;
        log.info("【原材料】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        for (MaterialsEnum materialsEnum : MaterialsEnum.values()) {
            if (!MaterialsEnum.CONCRETE.getCode().equals(materialsEnum.getCode())) {
                List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (quickConfigList.isEmpty()) {
                    log.info("【原材料快检】【{}】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料快检试验委托*/
                    this.createQuickMaterialsExperiment(checkTypeEnum, quickConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
                /**这里是批检的*/
                List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (batchConfigList.isEmpty()) {
                    log.info("【原材料批检】【{}】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料批检试验委托*/
                    this.createBatchMaterialsExperiment(checkTypeEnum, batchConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
            }
        }
        log.info("【原材料】{}定时任务执行完毕！", checkTypeEnum.getName());
    }

    /**
     * 年检  每年的1月1号凌晨1点执行
     */
    //@Scheduled(cron = " 0 0 1 1 1 *")
    public void genYear() {
        CheckTypeEnum checkTypeEnum = CheckTypeEnum.YEAR_CHECK;
        log.info("【原材料】{}定时任务执行开始！", checkTypeEnum.getName());
        /**这里是快检的*/
        for (MaterialsEnum materialsEnum : MaterialsEnum.values()) {
            if (!MaterialsEnum.CONCRETE.getCode().equals(materialsEnum.getCode())) {
                List<CheckQuickConfig> quickConfigList = this.getQuickConfigList(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (quickConfigList.isEmpty()) {
                    log.info("【原材料快检】【{}】暂无需要生成试验委托！快检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料快检试验委托*/
                    this.createQuickMaterialsExperiment(checkTypeEnum, quickConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
                /**这里是批检的*/
                List<CheckBatchConfig> batchConfigList = this.getBatchConfig(checkTypeEnum.getCode(), materialsEnum.getCode());
                if (batchConfigList.isEmpty()) {
                    log.info("【原材料批检】【{}】暂无需要生成试验委托！批检指标配置中没有【{}】的项目", materialsEnum.getName(), checkTypeEnum.getName());
                } else {
                    /**新增原材料批检试验委托*/
                    this.createBatchMaterialsExperiment(checkTypeEnum, batchConfigList, checkTypeEnum.getCode(), materialsEnum.getCode());
                }
            }
        }
        log.info("【原材料】{}定时任务执行完毕！", checkTypeEnum.getName());
    }


    /**
     * 新增原材料批检试验委托
     *
     * @param checkTypeEnum   检测类型枚举
     * @param quickConfigList 快检指标配置集合
     */
    private void createQuickMaterialsExperiment(CheckTypeEnum checkTypeEnum, List<CheckQuickConfig> quickConfigList, Integer checkType, Integer projectCategory) {
        try {
            /**获取所有供应商物料信息*/
            List<SupplierCompanyMaterials> supplierCompanyMaterialsList = supplierCompanyMaterialsMapper.getList(new SupplierCompanyMaterials().setMaterialsType(projectCategory));
            if (CollectionUtil.isNotEmpty(supplierCompanyMaterialsList)) {
                for (SupplierCompanyMaterials supplierCompanyMaterials : supplierCompanyMaterialsList) {
                    Experiment experiment = new Experiment();
                    experiment.setCheckType(CheckEnum.QUICK.getCode());
                    experiment.setExperimentType(projectCategory);
                    experiment.setCreateCheckType(checkType.intValue());
                    if (supplierCompanyMaterials.getSpecId() != null) {
                        if (StringUtils.isNotEmpty(supplierCompanyMaterials.getMaterialAbbreviation())) {
                            experiment.setSampleLevel(supplierCompanyMaterials.getMaterialAbbreviation() + supplierCompanyMaterials.getMaterialsConfigSpec());
                        } else {
                            experiment.setSampleLevel(supplierCompanyMaterials.getMaterialsConfigName() + supplierCompanyMaterials.getMaterialsConfigSpec());
                        }
                    } else {
                        experiment.setSampleLevel(supplierCompanyMaterials.getMaterialsName() + supplierCompanyMaterials.getMaterialsSpec());
                    }
                    experiment.setMaterialsId(String.valueOf(supplierCompanyMaterials.getMaterialsId()));
                    /**试验项目code集合*/
                    Set<String> testProjectCodeList = new HashSet<>();
                    for (CheckQuickConfig quickConfig : quickConfigList) {
                        /**放入试验项目code集合*/
                        List<String> codeList = Arrays.asList(quickConfig.getTestProjectCode().split(","));
                        testProjectCodeList.addAll(codeList);
                    }
                    experiment.setTestProjectCodeList(testProjectCodeList);
                    Long experimentId = experimentService.createRawMaterials(experiment);

                    /**保存原材料实验台账检查类型记录表*/
                    experiment.setId(experimentId);
                    experimentCheckTypeRecordService.saveExperimentCheckTypeRecord(experiment);
                }
            }
        } catch (Exception e) {
            log.error("【原材料快检】新增试验委托失败，检测类型：【{}】,失败原因：【{}】", checkTypeEnum.getName(), e.getMessage(), e);
        }
    }

    /**
     * 新增原材料批检试验委托
     *
     * @param checkTypeEnum
     * @param batchConfigList
     */
    private void createBatchMaterialsExperiment(CheckTypeEnum checkTypeEnum, List<CheckBatchConfig> batchConfigList, Integer checkType, Integer projectCategory) {
        try {
            /**获取所有供应商物料信息*/
            List<SupplierCompanyMaterials> supplierCompanyMaterialsList = supplierCompanyMaterialsMapper.getList(new SupplierCompanyMaterials().setMaterialsType(projectCategory));
            if (CollectionUtil.isNotEmpty(supplierCompanyMaterialsList)) {
                for (SupplierCompanyMaterials supplierCompanyMaterials : supplierCompanyMaterialsList) {
                    Experiment experiment = new Experiment();
                    experiment.setCheckType(CheckEnum.BATCH.getCode());
                    experiment.setExperimentType(projectCategory);
                    experiment.setCreateCheckType(checkType.intValue());
                    if (supplierCompanyMaterials.getSpecId() != null) {
                        if (StringUtils.isNotEmpty(supplierCompanyMaterials.getMaterialAbbreviation())) {
                            experiment.setSampleLevel(supplierCompanyMaterials.getMaterialAbbreviation() + supplierCompanyMaterials.getMaterialsConfigSpec());
                        } else {
                            experiment.setSampleLevel(supplierCompanyMaterials.getMaterialsConfigName() + supplierCompanyMaterials.getMaterialsConfigSpec());
                        }
                    } else {
                        experiment.setSampleLevel(supplierCompanyMaterials.getMaterialsName() + supplierCompanyMaterials.getMaterialsSpec());
                    }
                    experiment.setMaterialsId(String.valueOf(supplierCompanyMaterials.getMaterialsId()));
                    /**试验项目code集合*/
                    Set<String> testProjectCodeList = new HashSet<>();
                    for (CheckBatchConfig batchConfig : batchConfigList) {
                        /**放入试验项目code集合*/
                        List<String> codeList = Arrays.asList(batchConfig.getTestProjectCode().split(","));
                        testProjectCodeList.addAll(codeList);
                    }
                    experiment.setTestProjectCodeList(testProjectCodeList);
                    Long experimentId = experimentService.createRawMaterials(experiment);

                    /**保存原材料实验台账检查类型记录表*/
                    experiment.setId(experimentId);
                    experimentCheckTypeRecordService.saveExperimentCheckTypeRecord(experiment);
                }
            }
        } catch (Exception e) {
            log.error("【原材料快检】新增试验委托失败，检测类型：【{}】,失败原因：【{}】", checkTypeEnum.getName(), e.getMessage(), e);
        }
    }
}
